// 所有向后端请求的方法都写在这个里面
import {get, post} from "./axios";

export const getListPageUrl = "/page"
export const saveOrUpdateUrl = "/save/update"
export const removeByIdUrl = "/remove"
export const removeByIdsUrl = "/remove"

function setToken(param) {
    param.token = sessionStorage.getItem("manageToken");
}

/**
 * 通用的请求方法
 * @param url 请求路径
 * @param params 参数
 * @returns {Promise<unknown>}
 */
export const basePostRequest = (url, params) => post(url, params)
export const baseGetRequest = (url) => get(url)

/**
 *
 * @param moduleName 模块名称
 * @param action 自定义接口
 * @param params
 * @param that
 * @returns {Promise<T>}
 */
export const getTableData = (moduleName, params, that,action) => post(action ? moduleName + action : moduleName + getListPageUrl, params, that).then(res => {
        that.tableData = res.records;
        that.params.total = res.total;
    })

export const deleteById = (url, id, that) => get(url + removeByIdUrl + "/" + id).then(res => {
    that.baseSuccessTip(null, null, that);
})

export function deleteByIds (url, params, that){
    if (params && params.length > 0){
        this.$basePostRequest(url + removeByIdsUrl, params).then(res => {
            that.baseSuccessTip(null, null, that);
        });
    }else {
        that.notify("请先选择需要删除的数据!","error");
    }

}
export function saveData(url, params, that, formName) {
    this.$refs[formName].validate(valid => {
        if (!valid) {
            that.notify("输入格式不正确!", "error");
            return
        }
        setToken(params);
        this.$basePostRequest(url + saveOrUpdateUrl, params).then(res => {
            that.baseSuccessTip(null, null, that);
        });
        that.closeDia(params)
    })

}


